/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helpers;
/**
 *
 * @author arnaldo
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class ReadWrite {

    public static String read(String path) {

        String ret = "";

        try {

            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(path);

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {

                // Print the content on the console
//                System.out.println(strLine);
                ret+= strLine + "\n";
            }

            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        return ret;
    }

    public static void write(String data, String name) {
        try {
            // Create file
            FileWriter fstream = new FileWriter(name);
            BufferedWriter out = new BufferedWriter(fstream);

            out.write(data);

            //Close the output stream
            out.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
